import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;

public class Sound extends Canvas implements PlayerListener{
	private Display display;
	private Vibration vibration;
	private Player player;
	private VolumeControl vc;
	private InputStream is;
	public Sound(GameMIDlet midlet, Display display){
		this.display = display;	
		vibration = new Vibration(midlet, display);
		is = getClass().getResourceAsStream("/caralarm.mp3");
		try{			
			player = Manager.createPlayer(is,"audio/mpeg");	
			player.addPlayerListener(this);
			player.realize();								
			player.prefetch();	
			vc = (VolumeControl) player.getControl("VolumeControl");
			if(vc != null){
				vc.setLevel(100);
			}			
		
		}
		catch(Exception e){}	
		setFullScreenMode(true);
	}
	public void playerUpdate(Player player, String event, Object evenData){
		if(event == (PlayerListener.END_OF_MEDIA)){				
			//try{
				//player.stop();
			//}
			//catch(Exception e){}
		}		
	}
	public void paint(Graphics g){
		if(Option.language == true){
			g.setColor(255, 255, 255);
			g.fillRect(0, 0, 128, 160);
			g.setColor(0, 0, 0);
			g.drawString("Bat Am Thanh ?", 32, 65, 0);	
			g.drawString("Co", 2, 145, 0);
			g.drawString("Khong", 96, 145, 0);
		}
		else{
			g.setColor(255, 255, 255);
			g.fillRect(0, 0, 128, 160);
			g.setColor(0, 0, 0);
			g.drawString("Enable Sound ?", 32, 65, 0);	
			g.drawString("Yes", 2, 145, 0);
			g.drawString("No", 114, 145, 0);
		}
	}
	protected void keyPressed(int code){
		if(code == -6){
			Option.sound = true;
			try{
				player.start();
			}
			catch(Exception e){}
			display.setCurrent(vibration);
		}	
		if(code == -7){		
			Option.sound = false;	
			display.setCurrent(vibration);
		}	
	}
}